local PANEL = {}
PANEL.m_Player = NULL

function PANEL:Paint()
	local wid, hei = self:GetSize()

	surface.SetDrawColor(Color(5, 5, 5, 180))
	surface.DrawRect(0, 0, wid, hei)

	local pl = self:GetPlayer()
	local col
	if pl:IsValid() then
		col = team.GetColor(pl:Team())
	else
		col = color_white
	end

	local alpha
	if self.Depressed then
		alpha = 220
	elseif self.Hovered then
		alpha = math.abs(math.sin(RealTime() * 5)) * 220
	elseif pl == LocalPlayer() then
		alpha = math.abs(math.sin(RealTime() * 2)) * 220
	else
		alpha = 60
	end

	surface.SetDrawColor(col)

	surface.DrawOutlinedRect(0, 0, wid, hei)
end

function PANEL:DoClick()
	local pl = self:GetPlayer()
	if pl:IsValid() then
		gamemode.Call("ClickedPlayerButton", pl, self)
	end
end

function PANEL:Init()
	self:SetText(" ")

	self:SetPlayer(NULL)

	self.m_AvatarImage = vgui.Create("AvatarImage", self)
	self.m_NameLabel = EasyLabel(self, " ", "ScoreboardSub")
	self.m_ScoreLabel = EasyLabel(self, "Lap: 0/0", "ScoreboardSub")
	self.m_DeathLabel = EasyLabel(self, "Chck: 0/0", "ScoreboardSub")
	self.m_PingLabel = EasyLabel(self, "0", "ScoreboardSub")

	self:InvalidateLayout()
end

function PANEL:SetPlayer(pl)
	self.m_Player = pl

	self:Refresh()
end

function PANEL:GetPlayer()
	return self.m_Player
end

function PANEL:Refresh()
	local pl = self:GetPlayer()
	if pl:IsValid() then
		self.m_AvatarImage:SetPlayer(pl)
		self.m_AvatarImage:SetSize(64, 64)
		--self.m_AvatarImage:SetTooltip("Click here to view the Steam Community profile of "..pl:Name()..".")
		local tall = self:GetTall()
		if self.m_AvatarImage:GetTall() >= tall then
			if 32 < tall then
				self.m_AvatarImage:SetSize(32, 32)
			elseif 24 < tall then
				self.m_AvatarImage:SetSize(24, 24)
			elseif 16 < tall then
				self.m_AvatarImage:SetSize(16, 16)
			else
				self.m_AvatarImage:SetSize(tall - 2, tall - 2)
			end
		end

		self.m_NameLabel:SetText(pl:Name())
		self.m_NameLabel:SizeToContents()
		if pl:Alive() then
			self.m_NameLabel:SetTextColor(team.GetColor(pl:Team()))
		else
			self.m_NameLabel:SetTextColor(COLOR_GRAY)
		end

		self.m_ScoreLabel:SetText("Lap: ".. pl:Frags() .." / " ..GAMEMODE.LapsToWin-1)
		self.m_ScoreLabel:SizeToContents()
		self.m_ScoreLabel:SetColor(COLOR_GREEN)
		
		self.m_DeathLabel:SetText("Chck: ".. pl:Deaths() .." / " ..GAMEMODE.MaxCheckpoint)
		self.m_DeathLabel:SizeToContents()
		self.m_DeathLabel:SetColor(COLOR_YELLOW)
		
		if pl:Team() == TEAM_CROWD then
			self.m_ScoreLabel:SetVisible(false)
			self.m_DeathLabel:SetVisible(false)
		else
			self.m_ScoreLabel:SetVisible(true)
			self.m_DeathLabel:SetVisible(true)
		end

		local ping = pl:Ping()
		local colPing
		if 200 < ping then
			colPing = COLOR_RED
		elseif 150 < ping then
			colPing = COLOR_YELLOW
		elseif 100 < ping then
			colPing = color_white
		elseif 60 < ping then
			colPing = COLOR_CYAN
		else
			colPing = COLOR_LIMEGREEN
		end
		self.m_PingLabel:SetText(ping)
		self.m_PingLabel:SizeToContents()
		self.m_PingLabel:SetTextColor(colPing)
	end

	self:InvalidateLayout()
end

function PANEL:PerformLayout()
	local wid, hei = self:GetSize()
	local halfhei = hei * 0.5

	local x = 4

	self.m_AvatarImage:SetPos(x, halfhei - self.m_AvatarImage:GetTall() * 0.5)
	x = x + self.m_AvatarImage:GetWide() + 4

	if x + self.m_NameLabel:GetWide() >= wid * 0.4 then
		self.m_NameLabel:SetFont("ScoreboardText")
	else
		self.m_NameLabel:SetFont("ScoreboardSub")
	end
	self.m_NameLabel:SetPos(x, halfhei - self.m_NameLabel:GetTall() * 0.5)
	self.m_ScoreLabel:SetPos(wid * 0.4 - self.m_ScoreLabel:GetWide() * 0.45, halfhei - self.m_ScoreLabel:GetTall() * 0.5)
	self.m_DeathLabel:SetPos(wid * 0.7 - self.m_DeathLabel:GetWide() * 0.7, halfhei - self.m_DeathLabel:GetTall() * 0.5)
	self.m_PingLabel:SetPos(wid - self.m_PingLabel:GetWide() - 24, halfhei - self.m_PingLabel:GetTall() * 0.5)
end

vgui.Register("DPlayerPanel", PANEL, "DButton")